package com.cyou.qb.admin.web;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cyou.nad.datastore.DataList;
import com.cyou.nad.datastore.query.JdbcPage;
import com.cyou.nad.tag.table.TransCol;
import com.cyou.nad.utils.DateUtils;
import com.cyou.qb.common.domain.QiangbaAdmin;
import com.cyou.qb.common.service.StatisticsService;
import com.cyou.qb.constants.CodeTransConstants;

@Controller
@RequestMapping(value = "/statistics")
public class StatisticsController extends BaseController {

	@Resource(name = "StatisticsServiceImpl")
	private StatisticsService statisticsService;

	@RequestMapping(value = "/index")
	public String index(Model model, HttpServletRequest request, HttpServletResponse response) {
		//参数获取
		String activityName = StringUtils.trimToNull(request.getParameter("s_active_name")); //活动名称
		String activityStateStr = StringUtils.trimToNull(request.getParameter("s_active_state")); //活动状态 字符串0 1等
		String startDateStr = StringUtils.trimToNull(request.getParameter("s_date_start")); //搜索开始时间
		String endDateStr = StringUtils.trimToNull(request.getParameter("s_date_end")); //搜索结束时间
		//查询类型
		String searchType = StringUtils.trimToNull(request.getParameter("s_type")); //数据查询类型  null:默认  total:合计查询  detail:明细
		String activityId = StringUtils.trimToNull(request.getParameter("s_id")); //明细查询时 传入活动id

		//获取登陆用户信息
		QiangbaAdmin admin = (QiangbaAdmin) request.getSession().getAttribute("loginedUser");
		if (admin == null)
			return "login";//返回登陆页

		//保存参数 用于页面回传显示
		this.saveAllParameters(request);

		//参数处理 默认最近一个月
		Date endDate = null;
		Date startDate = null;
		int activityState = 0;//默认读取所有

		if (startDateStr != null) {
			startDate = DateUtils.parseDate(startDateStr, "yyyy-MM-dd");
		}
		if (endDateStr != null) {
			endDate = DateUtils.parseDate(endDateStr, "yyyy-MM-dd");
		}
		//判断时间，开始时间要晚于结束时间  时间不会出现为null的状况？
		if (startDate != null && endDate != null && startDate.after(endDate)) {
			model.addAttribute("error", "<span class='error'>结束日期大于开始日期</span>");
			return "statistics/index";
		}
		if (activityStateStr != null) {
			activityState = Integer.parseInt(activityStateStr);
		}

		try {
			//前台页面显示 活动状态 
			String activityStatus = CodeTransConstants.ACTIVITY_FACESTATUS.get(activityStateStr);

			//根据不同查询类型调用不同查询逻辑
			if (searchType == null) {//全部查询
				if (endDate == null) {
					endDate = org.apache.commons.lang.time.DateUtils.truncate(new Date(), Calendar.DATE);
					startDate = DateUtils.addMonth(endDate, -1);
				}
				//合计查询
				DataList total = statisticsService.findQueryTotal(admin, searchType, activityId, activityName,
						activityState, startDate, endDate);

				JdbcPage page = statisticsService.findPageQueryStatistics(admin, getPageNo(),
						getPageSize(DEFAULT_PAGE_SIZE), activityName, activityState, startDate, endDate);
				
				model.addAttribute("total", total);
				model.addAttribute("page", page);

				List<TransCol> trans = new ArrayList<TransCol>();
				trans.add(new TransCol("faceStatus", CodeTransConstants.ACTIVITY_FACESTATUS));
				model.addAttribute("trans", trans);

				//回传查询条件
				//避免首次访问时没有默认查询时间段
				model.addAttribute("s_date_start", DateUtils.formatDate(startDate, "yyyy-MM-dd"));
				model.addAttribute("s_date_end", DateUtils.formatDate(endDate, "yyyy-MM-dd"));
				return "statistics/index";

			} else if ("detail".equals(searchType)) {//详细查询
				//详细查询页 合计  不考虑活动状态，直接查库
				DataList total = statisticsService.findQueryTotal(admin, searchType, activityId, activityName,
						activityState, startDate == null ? null : startDate, endDate == null ? null : endDate);
				
				DataList dataList = statisticsService.findDetailStatistics(activityId, activityState,
						startDate == null ? null : startDate, endDate == null ? null : endDate);
				//model.addAttribute("name", activityName);
				model.addAttribute("total", total);
				model.addAttribute("status", activityStatus != null ? activityStatus : "无");
				model.addAttribute("dataList", dataList);
				//详情时间段
				model.addAttribute("startDate", startDateStr != null ? startDateStr : "无");
				model.addAttribute("endDate", endDateStr != null ? endDateStr : "无");
				return "statistics/detail";

			} else if ("total".equals(searchType)) {//全部查询
				//合计查询
				DataList total = statisticsService.findQueryTotal(admin, searchType, activityId, activityName,
						activityState, startDate == null ? null : startDate, endDate == null ? null : endDate);
				DataList dataList = statisticsService.findTotalStatistics(admin, activityState,
						startDate == null ? null : startDate, endDate == null ? null : endDate);
				
				model.addAttribute("total", total);
				model.addAttribute("status", activityStatus != null ? activityStatus : "无");
				model.addAttribute("dataList", dataList);
				model.addAttribute("startDate", startDateStr != null ? startDateStr : "无");
				model.addAttribute("endDate", endDateStr != null ? endDateStr : "无");

				return "statistics/total";
			}
		} catch (Exception e) {//无权限操作
			log.error(e.getMessage());
			return "login";
		}

		return "statistics/index";
	}

	public StatisticsService getStatisticsService() {
		return statisticsService;
	}

	public void setStatisticsService(StatisticsService statisticsService) {
		this.statisticsService = statisticsService;
	}
}
